package com.view.components.map
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	
	import mx.core.UIComponent;
	
	public class MapUI extends  UIComponent
	{
		private var rect:Rectangle;
		public function MapUI()
		{
			super();
						
			init();
		}
		private function init():void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete);
			loader.load(new URLRequest("asset/city_building_res.swf"));
		}
		//2325 1305

		private function complete(e:Event):void
		{
			var app:ApplicationDomain = LoaderInfo(e.currentTarget).applicationDomain;	
			var c1:Class = app.getDefinition("bg_down_right") as Class;
			var c2:Class = app.getDefinition("bg_down_left") as Class;
			var c3:Class = app.getDefinition("bg_top_left") as Class; 
			var c4:Class = app.getDefinition("bg_top_right") as Class;
			var mc_floorPart_c:Class = app.getDefinition("mc_floorPart") as Class;
			var bg_down_right:Bitmap = new Bitmap(new c1(0,0));
			var bg_down_left:Bitmap = new Bitmap(new c2(0,0));
			var bg_top_left:Bitmap = new Bitmap(new c3(0,0));
			var bg_top_right:Bitmap = new Bitmap(new c4(0,0));
			
			var sacle:Number = 0.7;
			bg_top_left.scaleX = sacle;
			bg_top_left.scaleY = sacle;
			this.addChild(bg_top_left);
			
			bg_down_left.x = 0;
			bg_down_left.y = bg_top_left.height;
			bg_down_left.scaleX = sacle;
			bg_down_left.scaleY = sacle;
			this.addChild(bg_down_left);
			
			bg_top_right.x = bg_top_left.width;
			bg_top_right.y = 0;
			bg_top_right.scaleX = sacle;
			bg_top_right.scaleY = sacle;
			this.addChild(bg_top_right);
			
			bg_down_right.x = bg_top_left.width;
			bg_down_right.y = bg_top_left.height;
			bg_down_right.scaleX = sacle;
			bg_down_right.scaleY = sacle;
			this.addChild(bg_down_right);
			
			createFloor(mc_floorPart_c,2,2);

			//create(c); 
			
			rect = new Rectangle(0,0,-bg_top_left.width,-bg_top_left.height);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}
	
		private function mouseDown(e:MouseEvent):void
		{
			this.startDrag(false,rect);
		}
		private function mouseUp(e:MouseEvent):void
		{
			this.stopDrag();
		}
		private function createFloor(c:Class,row:int,col:int):void
		{
			var initX:int = 800;
			var initY:int = 600;	
			var w:int,h:int;	
		   for(var i:int =0;i<row;i++)
		   {
		   	  for(var j:int = 0;j<col;j++)
		   	  {
		   	  	var tile:Sprite = new c();
		   	  	if(w==0&&h==0)
		   	  	{
			   	  	w = tile.width;
			   	  	h = tile.height;
			   	  }
		   	  	
		   	  	tile.x = initX + (j-i)*w*.5;
		   	  	tile.y = initY +(j+i)*h*.5;
		   	  	this.addChild(tile);
		   	  }
		   }
		   	this.x = -initY+w*.5;
			this.y = -initY+h*.5;
		}
		private function create(c:Class):void
		{
			var count:int = 20;
		   for(var i:int =0;i<count;i++)
		   {
		   	  for(var j:int = 0;j<count;j++)
		   	  {
		   	  	var tile:Bitmap = new Bitmap(new c(0,0));
		   	  	tile.x = 500+ (j-i)*tile.width*.5;
		   	  	tile.y = (j+i)*tile.height*.5;
		   	  	this.addChild(tile);
		   	  }
		   }	
		}

	}
}